package com.example.its2021606134chh.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.example.its2021606134chh.R;
import com.example.its2021606134chh.ThresholdModule.ThresholdSettingsFragment;
import com.example.its2021606134chh.ViewPageAdapter;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link IndexFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class IndexFragment extends Fragment {
    private TabLayout tabLayout;
    private ViewPager2 viewPager2;
    List<Fragment> fragmentList = new ArrayList<>();

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "username";
    private static final String ARG_PARAM2 = "param2";
    private static final String ARG_RECHARGE = "Recharge";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private Boolean recharge;

    public IndexFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment IndexFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static IndexFragment newInstance(String param1, String param2) {
        IndexFragment fragment = new IndexFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }
    public static IndexFragment newInstance(Boolean b, String param1, String param2) {
        IndexFragment fragment = new IndexFragment();
        Bundle args = new Bundle();
        args.putBoolean("Recharge", b);
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getArguments() != null) {
            recharge = getArguments().getBoolean(ARG_RECHARGE);
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_index, container, false);
        tabLayout = view.findViewById(R.id.tabLayout);
        viewPager2 = view.findViewById(R.id.viewPager2);
        if (recharge) {
//            Log.i("TAG", "recharge: True");
            fragmentList.add(PrepaidRecordsFragment.newInstance("充值记录", ""));
            fragmentList.add(PersonalInformationFragment.newInstance("个人信息", ""));
            fragmentList.add(ThresholdSettingsFragment.newInstance("阈值设置", ""));
        } else {
            fragmentList.add(PersonalInformationFragment.newInstance("个人信息", ""));
            fragmentList.add(PrepaidRecordsFragment.newInstance("充值记录", ""));
            fragmentList.add(ThresholdSettingsFragment.newInstance("阈值设置", ""));
        }
        ViewPageAdapter adapter = new ViewPageAdapter(getFragmentManager(), getLifecycle(), fragmentList);
        viewPager2.setAdapter(adapter);

        new TabLayoutMediator(tabLayout, viewPager2, true, new TabLayoutMediator.TabConfigurationStrategy() {
                @Override
                public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                    tab.setText(fragmentList.get(position).getArguments().getString("title"));
                }
        }).attach();
        return view;
    }
}